package com.gang.mars.redisson.Thread;

import lombok.SneakyThrows;

/**
 * 线程的状态
 * 1，创建
 * 2，就绪
 * 3，阻塞
 * 4，运行
 * 5，死亡
 * */
public class Apple05ThreadStop implements Runnable{

    /**
     * 线程停止，规范
     * 1，建议线程正常停止，利用次数/不建议死循环
     * 2，建议使用标识位，设置一下标识
     * 3，不再使用JDK弃用的方法，stop/destroy
     * */

    private static boolean flag =true;


    public static void main(String[] args) throws InterruptedException {

        Apple05ThreadStop status = new Apple05ThreadStop();

        new Thread(status).start();

        for (int i = 0; i < 10; i++) {
            Thread.sleep(5000);
            System.out.println("thread main sleep 5 i = " + i );
            if (i == 5) {
                System.out.println("thread ready stop...");
                status.stop();
                System.out.println("thread has stopped...");
            }
        }
    }

    @SneakyThrows
    @Override
    public void run() {

        int i = 0;

        while (flag){

            Thread.sleep(1000);

            System.out.println("thread son i=" + i++);
        }

    }

    public void stop(){
        flag = false;
    }




}
